package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.common.result.PageResult;
import com.itheima.common.result.Result;
import com.itheima.dto.RoleDTO;
import com.itheima.dto.RolePageQueryDTO;
import com.itheima.mapper.SysRoleMapper;
import com.itheima.mapper.SysRoleMenuMapper;
import com.itheima.pojo.SysMenu;
import com.itheima.pojo.SysRole;
import com.itheima.pojo.SysRoleMenu;
import com.itheima.service.SysRoleService;
import com.itheima.vo.RoleVo;
import com.itheima.vo.SysMenuVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    SysMenuServiceImpl sysMenuService;
    /**
     * 角色列表分页
     *
     * @param roleDTO
     * @return
     */
    @Override
    public PageResult pageQuery(RolePageQueryDTO roleDTO) {
        IPage<SysRole> page = new Page<>(roleDTO.getPage(), roleDTO.getLimit());
        LambdaQueryWrapper<SysRole> lqwRole = new LambdaQueryWrapper<>();
        lqwRole.like(SysRole::getName,roleDTO.getName());
        page(page,lqwRole);
        List<SysRole> records = page.getRecords();
        List<RoleVo> list = new ArrayList<>();
        records.forEach(rs->{
            LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper();
            lqw.select(SysRoleMenu::getMenuId);
            lqw.eq(SysRoleMenu::getRoleId,rs.getId());
            List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(lqw);
            List<String> collect = sysRoleMenus.stream().map(srs -> srs.getMenuId()).collect(Collectors.toList());
            RoleVo roleVo = RoleVo.builder()
                    .menuids(collect)
                    .role_id(rs.getId())
                    .build();
            BeanUtils.copyProperties(rs,roleVo);
            list.add(roleVo);
        });

        return new PageResult(page.getTotal(),page.getCurrent(),list,page.getSize(),page.getPages()) ;
    }
    /**
     * 角色详情
     * @param id
     * @return
     */
    @Override
    public SysRole rowInfo(String id) {
        SysRole role = getById(id);
        RoleVo roleVo = new RoleVo();
        BeanUtils.copyProperties(role,roleVo);

        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper();
        lqw.select(SysRoleMenu::getMenuId);
        lqw.eq(SysRoleMenu::getRoleId,id);
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(lqw);
        //获取第一层菜单
        List<SysMenu> sysMenus = new ArrayList<>();
        sysRoleMenus.forEach(s->{
            LambdaQueryWrapper<SysMenu> lqw2 = new LambdaQueryWrapper();
            lqw2.eq(SysMenu::getPid,0);
            lqw2.eq(SysMenu::getId,s.getMenuId());
            SysMenu sysMenu = sysMenuService.getOne(lqw2);
            if(sysMenu !=null) {
                sysMenus.add(sysMenu);
            }
        });
        List<SysMenuVo> sysMenuVos = listVo(sysMenus,id);
        roleVo.setAuthList(sysMenuVos);
        return roleVo;
    }

    public List<SysMenuVo> listVo(List<SysMenu> list,String id){
        List<SysMenuVo> sysMenuVos = new ArrayList<>();
        //添加子数据
        list.forEach(s->{
            SysMenuVo sysMenuVo = new SysMenuVo();
            BeanUtils.copyProperties(s,sysMenuVo);
            List<SysMenu> list1 = sysRoleMenuMapper.selectMenus(id,s.getId());
            if(list1.size() == 0){
                sysMenuVos.add(sysMenuVo);
                return;
            }
            sysMenuVo.setChildren(listVo(list1,id));
            sysMenuVos.add(sysMenuVo);
        });


        return sysMenuVos;
    }

    /**
     * 获取角色列表-全部
     * @return
     */
    @Override
    public Result roleAllData() {

        List<SysRole> roleList = list();

        return Result.success(roleList);

    }

    /**
     * 角色修改
     *
     * @param
     * @return
     */
    @Override
    @Transactional
    public void updateRole(RoleDTO dto) {
        SysRole role = new RoleDTO();
        BeanUtils.copyProperties(dto,role);
        updateById(role);
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysRoleMenu::getRoleId,role.getId());
        sysRoleMenuMapper.delete(lqw);
        dto.getMenuids().forEach(s->{
            SysRoleMenu srm = new SysRoleMenu();
            srm.setRoleId(role.getId());
            srm.setMenuId(s);
            sysRoleMenuMapper.insert(srm);
        });
    }

    /**
     * 角色添加
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public void addRole(RoleDTO dto) {
        dto.setCreateDate(LocalDateTime.now());
        dto.setUpdateDate(LocalDateTime.now());
        save(dto);
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysRoleMenu::getRoleId,dto.getId());
        dto.getMenuids().forEach(s -> {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setMenuId(s);
            roleMenu.setRoleId(dto.getId());
            sysRoleMenuMapper.insert(roleMenu);
        });
    }

    /**
     * 角色删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public void delRole(String id) {
        removeById(id);
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysRoleMenu::getRoleId,id);
        sysRoleMenuMapper.delete(lqw);
    }
}
